package com.nxy.cpfsfe.busi.bolag;


import com.adtec.starring.datapool.CompSDO;
import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.nxy.cpfsfe.config.CpfsFeAllConfig;
import com.nxy.cpfsfe.enumtype.*;
import com.nxy.cpfsfe.mybatis.mapper.*;
import com.nxy.cpfsfe.mybatis.pojo.*;
import com.nxy.cpfsfe.service.CpfsService;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * Description:通用处理服务Bean(900报文)
 *
 * @author
 * @data 2019年8月24日
 */

public class NCSGenalProcConfirmSrvBean  implements CpfsService {
	
	
	private final static Logger LOGGER = LoggerFactory.getLogger(NCSGenalProcConfirmSrvBean.class);
	SqlSession sqlSession = null;	

	/**
	 * 异步处理
	 * Description:通用处理服务
	 *
	 * @author
	 * @data 2019年8月24日
	 */
	
	public void  doService() {
		String tpId = DtaInfo.getInstance().getTpId();
		sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");		
		// 获取输入
		CompSDO inputSdo = (CompSDO) EPOper.getCompSDO(tpId, "ReqBody"); // 获取输入接口				
		// 业务报文			
		String bizMsg = (String) inputSdo.getValue("BizMsg")!=null?(String) inputSdo.getValue("BizMsg"):"";
		String bizSta = StatEnum.BIZSTA_ING.getCode();//处理中	
		LOGGER.info("接收到输入BizMsg报文数据：[{}]" , bizMsg);
				
		String msgCd =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/msgCd");	
		String refMsgCd =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refMsgCd");  
		String rcvMbrCd =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/rcvMbrCd");
		String prcSts =XmlUtil.getValByXPath(bizMsg,"/transaction/body/CmonConf/CmonConfInf/PrcSts");
		String rjctInf =XmlUtil.getValByXPath(bizMsg,"/transaction/body/CmonConf/CmonConfInf/RjctInf");
		String prcCd =XmlUtil.getValByXPath(bizMsg,"/transaction/body/CmonConf/CmonConfInf/PrcCd");
		if(!MsgCdOrTpEnum.MSGCD_900.getCode().equals(msgCd)){
			LOGGER.warn("NCS通用处理确认报文号应为:[{}]，此报文号：[{}]非法",MsgCdOrTpEnum.MSGCD_900.getCode(),msgCd);
			return;
		}
		if(StringUtils.isEmpty(refMsgCd)){
			LOGGER.warn("NCS通用处理确认报文原报文号为空");
			return;
		}
		Map<String, Object> map  = xml2BizMap(bizMsg);	
		map.put("refMsgCd",refMsgCd);
		//云支付前置需要处理的回执报文需写在if()或者else if()里，其余NPS.900.001.01暂时不作处理,返回给业务系统
		LOGGER.info("开始处理{}交易的通用处理确认",refMsgCd);
		if(MsgCdOrTpEnum.MSGCD_120.getCode().equals(refMsgCd)){
			//PR00：已清算, PR07：已排队, PR14: 排队已撤销，  PR01：已拒绝， PR08：已撤销， PR09：已存款冲销， PR10：已补正
			if(StatEnum.STAR_PR00.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_SUC.getCode();//交易成功00
			}else if(StatEnum.STAR_PR01.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_FAIL.getCode();//交易失败01
			}else{
				bizSta = StatEnum.BIZSTA_ING.getCode();//交易处理中02
			}
			map.put("bizSta",bizSta);
			updateStat(sqlSession,map,bizMsg);

			//向Nginx发送通知
			respNginx(sqlSession,map);
		
		}else if(MsgCdOrTpEnum.MSGCD_140.getCode().equals(refMsgCd)){
			//TODO每个场景单独处理
			if(StatEnum.STAR_PR00.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_SUC.getCode();//交易成功00
			}else if(StatEnum.STAR_PR01.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_FAIL.getCode();//交易失败01
			}else{
				bizSta = StatEnum.BIZSTA_ING.getCode();//交易处理中02
			}
			map.put("bizSta",bizSta);
			updateStat(sqlSession,map,bizMsg);
		}else if(MsgCdOrTpEnum.MSGCD_142.getCode().equals(refMsgCd)){
			//TODO每个场景单独处理
			if(StatEnum.STAR_PR00.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_SUC.getCode();//交易成功00
			}else if(StatEnum.STAR_PR01.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_FAIL.getCode();//交易失败01
			}else{
				bizSta = StatEnum.BIZSTA_ING.getCode();//交易处理中02
			}
			map.put("bizSta",bizSta);
			updateStat(sqlSession,map,bizMsg);
		}else if(MsgCdOrTpEnum.MSGCD_805.getCode().equals(refMsgCd)){
			CpfsFeAllConfig.jedisCluster.publish((String)map.get("/transaction/header/msg/refSndMbrCd")+(String)map.get("/transaction/header/msg/refSeqNb"),bizMsg);
			LOGGER.info("redis消息发布者={}",(String)map.get("/transaction/header/msg/refSndMbrCd")+(String)map.get("/transaction/header/msg/refSeqNb"));	
			//成员行登录退出应答处理
			LOGGER.debug("NCS登录应答报文标识号应为:[{}]，此报文标识号为：[{}]",MsgCdOrTpEnum.MSGCD_900.getCode(),msgCd);
			LOGGER.info("NCS返回响应码为:[{}],拒绝信息为:[{}]",prcCd,rjctInf);
			returnNPS990(map);
		}else if(MsgCdOrTpEnum.MSGCD_903.getCode().equals(refMsgCd)){
			//证书绑定应答报文处理
			String msgId =XmlUtil.getValByXPath(bizMsg,"/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlMsgId");
			CpfsFeAllConfig.jedisCluster.publish((String)map.get("/transaction/header/msg/refSndMbrCd")+(String)map.get("/transaction/header/msg/refSeqNb"),bizMsg);
			LOGGER.info("redis消息发布者={}",(String)map.get("/transaction/header/msg/refSndMbrCd")+(String)map.get("/transaction/header/msg/refSeqNb"));
			String str = CpfsFeAllConfig.jedisCluster.get(msgId);
			String chgTp = str.split("\\|")[0];
			String crdlDN = str.split("\\|")[1];
			LOGGER.debug("Redis字符串:[{}]" , str);
			LOGGER.debug("绑定标识:[{}]" , chgTp);
			LOGGER.debug("DN值:[{}]" , crdlDN);
        	if(chgTp !=null && chgTp.length()>0){
        		updateDgtlCrdl(rcvMbrCd,prcSts,rjctInf,chgTp,crdlDN);					
			}else{
				LOGGER.error("操作类型为空，请重新发送请求");
			}
			returnNPS990(map);
        }else if(MsgCdOrTpEnum.MSGCD_303.getCode().equals(refMsgCd)){
			CpfsFeAllConfig.jedisCluster.publish((String)map.get("/transaction/header/msg/refSndMbrCd")+(String)map.get("/transaction/header/msg/refSeqNb"),bizMsg);
			LOGGER.info("redis消息发布者=[{}]",(String)map.get("/transaction/header/msg/refSndMbrCd")+(String)map.get("/transaction/header/msg/refSeqNb"));
			try {
				Map<String, String> bizmap = new HashMap<String, String>();
				if(StatEnum.STAR_PR02.getCode().equals(map.get("respSta"))||StatEnum.STAR_PR00.getCode().equals(map.get("respSta"))){
					bizmap.put("bizSta", StatEnum.BIZSTA_SUC.getCode());
				}
				else {					
					bizmap.put("bizSta", StatEnum.BIZSTA_FAIL.getCode());
				}
				bizmap.put("sndOrgNo", (String)map.get("/transaction/header/msg/refSndMbrCd"));
				bizmap.put("seqNb", (String)map.get("/transaction/header/msg/refSeqNb"));
				ToSysNtcMapper toSysNtc = sqlSession.getMapper(ToSysNtcMapper.class);
				toSysNtc.update(bizmap);
			} catch (Throwable e) {
				LOGGER.error("303自由格式ToSysNtc表更新异常,SeqNb=["+(String)map.get("/transaction/header/msg/refSeqNb")+"]",e);
			}
			returnNPS990(map);
        } else if(MsgCdOrTpEnum.MSGCD_365.getCode().equals(refMsgCd)){
        	if(StatEnum.STAR_PR00.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_SUC.getCode();//交易成功00
			}else if(StatEnum.STAR_PR01.getCode().equals(map.get("respSta"))){
				bizSta = StatEnum.BIZSTA_FAIL.getCode();//交易失败01
			}else{
				bizSta = StatEnum.BIZSTA_ING.getCode();//交易处理中02
			}
			map.put("bizSta",bizSta);
			updateTbMnyAStat(sqlSession,map);
			String refSeqNb =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refSeqNb");
			String sndAppCd = XmlUtil.getSysIdBySeqNb(refSeqNb);
			if(SysEcodeEnum.EPAY.getCode().equals(sndAppCd) || SysEcodeEnum.EMAN.getCode().equals(sndAppCd)){
				returnGWGenalProcConfirm(bizMsg,sndAppCd);
			}else{
				returnNPS990(map);
			}



		}else {
			//其他报文需根据refSeqNb字段获取原交易的发起的业务系统
			String refSeqNb =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/refSeqNb");
			String sndAppCd = XmlUtil.getSysIdBySeqNb(refSeqNb);
			if(SysEcodeEnum.EPAY.getCode().equals(sndAppCd) || SysEcodeEnum.EMAN.getCode().equals(sndAppCd)){
				returnGWGenalProcConfirm(bizMsg,sndAppCd);			
			}else{
				returnNPS990(map);
			}
		}
					
	}
	
	public void updateDgtlCrdl(String rcvMbrCd,String prcSts,String rjctInf,String chgTp,String crdlDN){
		//获取事务控制管理器
		sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");
		try{
			ToNcsCrdl toNcsCrdl = new ToNcsCrdl();
			toNcsCrdl.setBankNo(rcvMbrCd);	
			toNcsCrdl.setCrdlDN(crdlDN);	
			ToNcsCrdlMapper toNcsCrdlMap = sqlSession.getMapper(ToNcsCrdlMapper.class);
			//更新ncs证书表中的绑定标识	
			if(StatEnum.CHANGETYPE_CC00.getCode().equals(chgTp)&&StatEnum.STAR_PR02.getCode().equals(prcSts)){					
				toNcsCrdl.setBndFlg("CC00");  //绑定	
				toNcsCrdl.setBndTm(new Date());
				toNcsCrdlMap.update(toNcsCrdl);
				//更新机构表中的DN值
				ToOrg toOrg = new ToOrg();
				toOrg.setNcsCrdDN(crdlDN);
				toOrg.setUpdTm(new Date());
				toOrg.setOrgBNo(rcvMbrCd);
				ToOrgMapper toOrgMap = sqlSession.getMapper(ToOrgMapper.class);
				toOrgMap.update(toOrg);
				//刷新时间戳
				TpRefrshTblnm tblnm = new TpRefrshTblnm();
				tblnm.setTblNm("to_org");
				tblnm.setUpdtTime(DateUtil.getDateTime());
				sqlSession.getMapper(TpRefrshTblnmMapper.class).update(tblnm);
				LOGGER.info("修改机构表的DN值成功，刷新时间戳成功");
			}else if(StatEnum.CHANGETYPE_CC02.getCode().equals(chgTp)&&StatEnum.STAR_PR02.getCode().equals(prcSts)){
				toNcsCrdl.setBndFlg("CC02");  //解绑
				toNcsCrdl.setUnBndTm(new Date());
				toNcsCrdlMap.update(toNcsCrdl);
			}else{
				LOGGER.info("NCS返回拒绝信息为:[{}]",rjctInf);
			}

		}catch(Throwable e){
			LOGGER.error("更新数据失败",e);
			throw new BaseException(RespCd.NCPS5006,e);
		}
		
	}
	
	//更新原交易状态
	private void updateStat(SqlSession sqlSession,Map map,String bizMsg) {

		try{			
			TbA tbA = queryOrgAgSeq(sqlSession, map);
			if(tbA!=null){
				map.put("cpfsSeqNo",tbA.getCpfsSeqNo());//前置流水号
			}
			TsPCmd tsPCmd = queryOrgBizSeq(sqlSession, map);
			if(tbA!=null && tsPCmd!=null){
				map.put("updTm", DateUtil.getTimestamp(new Date()));
				map.put("orgnBizSta",tbA.getBizSta());
				//更新支付流水交易状态
				int tsPCmdUpdateNum = sqlSession.getMapper(TsPCmdMapper.class).updateByMap(map);
				//更新账务流水交易状态
				int tbAUpdateNum = sqlSession.getMapper(TbAMapper.class).updateByMap(map);
				LOGGER.info("账务流水表TbA更新数据成功：[{}]条，cpfsSeqNo=[{}];支付流水表tsPCmd更新数据成功：[{}]条，cpfsSeqNo=[{}]",tbAUpdateNum,tbA.getCpfsSeqNo(),tsPCmdUpdateNum,tsPCmd.getCpfsSeqNo());
				if(tbAUpdateNum==0 && tsPCmdUpdateNum==0){
					LOGGER.error("更新数据异常：业务流水表TbA和支付流水表tsPCmd,更新数据条数不符合预期：cpfsSeqNo："+map.get("chlSeqNo"));
					return;
				}
				//原交易是批量CPFS的汇兑往账不需转发网关,新e支付也不需要转网关
				if(SysEcodeEnum.EPAY.getCode().equals(tbA.getSndSysId()) || SysEcodeEnum.EMAN.getCode().equals(tbA.getSndSysId())){
					returnGWGenalProcConfirm(bizMsg,tbA.getSndSysId());
				}else{
					returnNPS990(map);
				}
			}else{
				returnNPS990(map);
				LOGGER.error("原交易不存在处理方式待人工复查确认,cpfsSeqNo：" + (String)map.get("chlSeqNo"));
			}
		
		}catch (Throwable e) {
			returnNPS990(map);
			LOGGER.error("更新数据失败：业务流水表TbA和支付流水表tsPCmd：cpfsSeqNo:"+(String)map.get("chlSeqNo"),e);
		}
	}

	//更新账务流水状态
	private void updateTbAStat(SqlSession sqlSession,Map map) {
		try{
			TbA tbA = queryOrgAgSeq(sqlSession, map);
			if(tbA!=null){
				//更新账务流水交易状态
				map.put("updTm", DateUtil.getTimestamp(new Date()));
				map.put("orgnBizSta",tbA.getBizSta());
				map.put("respMsgTyp",MsgCdOrTpEnum.MSGCD_900.getCode());
				int tbAUpdateNum = sqlSession.getMapper(TbAMapper.class).updateByMap(map);
				LOGGER.info("更新数据成功：[{}]条，账务流水表TbA：cpfsSeqNo=[{}]",tbAUpdateNum,tbA.getCpfsSeqNo());
				if(tbAUpdateNum==0){
					LOGGER.error("更新数据异常：账务流水表TbA,更新数据条数不符合预期：cpfsSeqNo："+map.get("chlSeqNo"));
					return;
				}
			}else{
				LOGGER.error("原交易不存在处理方式待确认,cpfsSeqNo=" + (String)map.get("chlSeqNo"));
			}
		}catch (Throwable e) {
			LOGGER.error("更新数据失败：账务流水表TbA：cpfsSeqNo："+map.get("chlSeqNo"),e);
		}
	}
	
	//更新资金调拨流水状态
	private void updateTbMnyAStat(SqlSession sqlSession,Map map) {
		try{
			TbMnyAprtA tbMnyAprtA = queryMnyAgSeq(sqlSession, map);
			if(tbMnyAprtA!=null){
				map.put("updTm", DateUtil.getTimestamp(new Date()));	
				map.put("orgnBizSta",tbMnyAprtA.getBizSta());
				map.put("respMsgTyp",MsgCdOrTpEnum.MSGCD_900.getCode());
				int tbAUpdateNum = sqlSession.getMapper(TbMnyAprtAMapper.class).updateByMap(map);
				LOGGER.debug("更新数据成功：[{}]条，资金调拨表TbMnyAprtA：cpfsSeqNo=[{}]",tbAUpdateNum,tbMnyAprtA.getCpfsSeqNo());
				if(tbAUpdateNum==0){
					LOGGER.error("更新数据异常：资金调拨表TbMnyAprtA,更新数据条数不符合预期：cpfsSeqNo："+map.get("chlSeqNo"));
					return;
				}
			}else{
				LOGGER.error("原交易不存在处理方式待确认,cpfsSeqNo:"+(String)map.get("chlSeqNo"));
			}
		
		}catch (Throwable e) {
			// TODO: handle exception
			LOGGER.error("更新数据失败：资金调拨表TbMnyAprtA：cpfsSeqNo:"+map.get("chlSeqNo"),e);
		}
	}
	

	//查询账务信息
	private TbA queryOrgAgSeq(SqlSession sqlSession,Map map) {
		TbAMapper tbAMapper = sqlSession.getMapper(TbAMapper.class);
		TbA tbA = new TbA();
		//tbA.setCpfsSeqNo((String)map.get("cpfsSeqNo"));
		tbA.setChlSeqNo((String)map.get("chlSeqNo"));
		tbA.setAcqOrgId((String)map.get("acqOrgId"));
		TbA tbAResult = tbAMapper.query(tbA);
		return tbAResult;
	}

	//查询资金调拨信息
	private TbMnyAprtA queryMnyAgSeq(SqlSession sqlSession,Map map) {
		TbMnyAprtAMapper tbMnyAMapper = sqlSession.getMapper(TbMnyAprtAMapper.class);
		TbMnyAprtA tbMnyAprtA = new TbMnyAprtA();
		//tbMnyAprtA.setCpfsSeqNo((String)map.get("cpfsSeqNo"));
		tbMnyAprtA.setChlSeqNo((String)map.get("chlSeqNo"));
		tbMnyAprtA.setAcqOrgId((String)map.get("acqOrgId"));
		TbMnyAprtA tbMnyAResult = tbMnyAMapper.query(tbMnyAprtA);
		return tbMnyAResult;
	}
	
	//查询支付信息
	private TsPCmd queryOrgBizSeq(SqlSession sqlSession,Map map) {	
		TsPCmdMapper tsPCmdMapper = sqlSession.getMapper(TsPCmdMapper.class);
		TsPCmd tsPCmd = new TsPCmd();	
		tsPCmd.setCpfsSeqNo((String)map.get("cpfsSeqNo"));
		tsPCmd.setAcqOrgId((String)map.get("acqOrgId"));
		TsPCmd tsPCmdResult = tsPCmdMapper.query(tsPCmd);				
		return tsPCmdResult;

	}
	
	
	public void returnGWGenalProcConfirm(String bizMsg,String sndAppCd) {

	     String tpId = DtaInfo.getInstance().getTpId();
	     String msgCd =XmlUtil.getValByXPath(bizMsg,"/transaction/header/msg/msgCd");
	   
		 EPOper.put(tpId, "ReqBean[0].head[0].rcvAppCd", sndAppCd);
    	 EPOper.put(tpId, "ReqBean[0].head[0].reqServCd", msgCd);
	     EPOper.getCompSDO(tpId, "ReqBody").setValue("BizMsg",bizMsg);
	     RpcCommUtil.getInstance().rpcFeToGW(tpId, RpcOutputInfoEnum.OutputName_Pub_Cpfsgw.getCode(),RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());
			
	}

	public void returnNPS990(Map bizMap){
		String tpId = DtaInfo.getInstance().getTpId();
		//转发给网关
		try{
			String bizMsg990 = ResponseUtil.build990Xml(bizMap);
			LOGGER.debug("rpc调用网关发送NPS990开始......");
			EPOper.put(tpId, "ReqBean[0].head[0].rcvAppCd", SysEcodeEnum.NCS.getCode());
			EPOper.put(tpId, "ReqBean[0].head[0].reqServCd", MsgCdOrTpEnum.MSGCD_990.getCode());
			EPOper.getCompSDO(tpId, "ReqBody").setValue("BizMsg", bizMsg990);
			RpcCommUtil.getInstance().rpcFeToGW(tpId, RpcOutputInfoEnum.OutputName_Pub_Cpfsgw.getCode(),RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());
		}catch(Exception e){
			LOGGER.error("调用网关转发NPS.990报文服务失败",e);
			throw new BaseException(RespCd.NCPO6047,e,"调用网关服务异常");
		}
	}

	/**
	 *	根据原支付流水的报文编号，向Nginx返回响应
	 */
	private void respNginx(SqlSession sqlSession,Map<String, Object> map){

		TsPCmd origTsPCmdQR = null;
		TbA origTbAQR = null;
		try{
			TsPCmdMapper tsPCmdMapper = sqlSession.getMapper(TsPCmdMapper.class);
			TbAMapper tbAMapper = sqlSession.getMapper(TbAMapper.class);

			TbA origTbAQC = new TbA();
			origTbAQC.setChlSeqNo((String)map.get("chlSeqNo"));
			origTbAQC.setAcqOrgId((String)map.get("acqOrgId"));
			origTbAQR = tbAMapper.query(origTbAQC);

			TsPCmd origTsPCmdQC = new TsPCmd();
			origTsPCmdQC.setCpfsSeqNo(origTbAQR.getCpfsSeqNo());
			origTsPCmdQC.setAcqOrgId(origTbAQR.getAcqOrgId());
			origTsPCmdQR = tsPCmdMapper.query(origTsPCmdQC);

			if(MsgCdOrTpEnum.MSGTD_103.getCode().equals(origTsPCmdQR.getMsgTp())||MsgCdOrTpEnum.MSGTD_102.getCode().equals(origTsPCmdQR.getMsgTp())){
				sndNginxResp(origTsPCmdQR,origTbAQR);
			}
		}catch (BaseException e){
			throw e;
		}catch (Exception e){
			String errMsg = String.format("向业务系统[%s]返回[%s]的响应[CPFS.501或CPFS.502]失败，chlSeqNo=[%s]，[%s]",origTsPCmdQR.getBizSysId(),origTsPCmdQR.getMsgTp(),origTbAQR.getChlSeqNo(),e.getMessage());
			throw new BaseException(RespCd.NCPO0005,e,errMsg);
		}

	}

	/**
	 *	发送Nginx响应
	 */
	private void sndNginxResp(TsPCmd tsPCmd,TbA tbA) throws Exception{

		String resp501Msg = buildNginxResp(tsPCmd, tbA, MsgCdOrTpEnum.MSGTD_501.getCode());
		String resp502Msg = buildNginxResp(tsPCmd, tbA, MsgCdOrTpEnum.MSGTD_502.getCode());

		JSONObject addInfo = JSONObject.parseObject(tsPCmd.getAddInfo());

		Map<String,String> params = new HashMap<String,String>();
		params.put("/root/MsgHeader/MsgTp",tsPCmd.getMsgTp());
		params.put("/root/MsgHeader/CorrId",tsPCmd.getComMsgId());
		params.put("/root/MsgHeader/SndSysId",tsPCmd.getBizSysId());
		params.put("/root/MsgHeader/SndBankId",tsPCmd.getSndBNo());
		params.put("NginxUrl",addInfo.getString("NginxUrl"));
		params.put("/root/MsgHeader/ClbckUrl",addInfo.getString("ClbckUrl"));

		HttpUtil.responseSendJson(resp501Msg,resp502Msg,params);

		LOGGER.info("向业务系统[{}]返回[{}]的响应[CPFS.501或CPFS.502]成功，chlSeqNo=[{}]",tsPCmd.getBizSysId(),tsPCmd.getMsgTp(),tbA.getChlSeqNo());

	}

	/**
	 * 	构建响应Nginx的报文
	 */
	private String buildNginxResp(TsPCmd tsPCmd,TbA tbA,String msgTp) throws Exception{

		Map<String,String> buildMap = new HashMap<String,String>();
		buildMap.put("RcvSysId", tsPCmd.getBizSysId());
		buildMap.put("SndBankId",tsPCmd.getRcvBNo());
		buildMap.put("RcvBankId",tsPCmd.getSndBNo());
		buildMap.put("CorrId",tsPCmd.getComMsgId());
		buildMap.put("TrxId",tsPCmd.getXSeqNo());
		buildMap.put("OriTrxId",tsPCmd.getXSeqNo());
		buildMap.put("TrxStsCd",RespCd.NCPI0000);
		buildMap.put("BizStsCd",tsPCmd.getBizSta());
		if(StatEnum.BIZSTA_SUC.getCode().equals(tsPCmd.getBizSta())){
			buildMap.put("BizStsDesc",StatEnum.BIZSTA_SUC.getName());
		}else{
			buildMap.put("BizStsDesc",tbA.getErrDesc());
		}
		buildMap.put("MsgId",tbA.getChlSeqNo());
		buildMap.put("EndToEndId",tsPCmd.getEndToEndId());
		buildMap.put("SttlmDt",DateUtil.date4Str(tsPCmd.getCpfsD()));
		buildMap.put("TrxPurpose",tsPCmd.getpInfo());
		buildMap.put("TrxRemark",tbA.getReserved1());

		if(MsgCdOrTpEnum.MSGTD_501.getCode().equals(msgTp)){
			buildMap.put("InstalAmt","0.00");

			return ResponseUtil.buildGeneral501Xml(buildMap);
		}else{
			buildMap.put("TrxCtgy",tsPCmd.getBizTyp());
			buildMap.put("TxNcsId",tbA.getChlSeqNo());
			buildMap.put("TrxAmt",tsPCmd.getOrdAmt().toPlainString());
			buildMap.put("TrxSysId",tbA.getpCId());
			buildMap.put("TrxChnlTp","07");

			return ResponseUtil.buildGeneral502Xml(buildMap);
		}

	}

	public Map xml2BizMap (String bizMsg){
	    //拆解xml报文为map
		Map<String, Object> map=null;
		try{
			Map xml2BizMap = XmlUtil.xml2Map(bizMsg);
			//把map<String, String>转换成map<String, Object>
			map = xml2BizMap;	
			//map.put("cpfsSeqNo",(String)map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlMsgId"));//原报文标识号
			map.put("chlSeqNo",(String)map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlMsgId"));//原报文标识号
			//map.put("xSeqNo",(String)map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlMsgId"));//原报文标识号
			map.put("sndBNo",(String)map.get("/transaction/header/msg/refSndMbrCd"));//原发起行行号TsPcmd
			map.put("acqOrgId",(String)map.get("/transaction/header/msg/refSndMbrCd"));//acqOrgId
			map.put("sndOrgId",(String)map.get("/transaction/header/msg/refSndMbrCd"));//原发起行行号TbA
			map.put("chlXCd",(String)map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlMT"));//原报文编号
			String drcrFlg = (String) map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlBizTyp");//原借贷标识
			drcrFlg = drcrFlg != null ? drcrFlg.toString() : "";
			if(drcrFlg.startsWith("A")||drcrFlg.startsWith("C")){
				drcrFlg=TAbEnum.DrcrFlag_C.getCode();//贷记
			}else if (drcrFlg.startsWith("B")||drcrFlg.startsWith("D")){
				drcrFlg=TAbEnum.DrcrFlag_D.getCode();//借记
			}else {
				drcrFlg="";
			}			
			map.put("drcrFlg",drcrFlg);//原借贷标识
			map.put("bizTyp",(String)map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlBizTyp"));//原业务类型
			map.put("bizCls",(String)map.get("/transaction/body/CmonConf/OrgnlGrpHdr/OrgnlBizKind"));//原业务种类
			map.put("xDirn", TAbEnum.XDirn_In.getCode());
			map.put("respMsgTyp",(String)map.get("/transaction/header/msg/msgCd"));		
			map.put("respSta",(String)map.get("/transaction/body/CmonConf/CmonConfInf/PrcSts"));
			map.put("errCd",(String)map.get("/transaction/body/CmonConf/CmonConfInf/PrcCd"));
			map.put("errDesc",(String)map.get("/transaction/body/CmonConf/CmonConfInf/RjctInf"));
			//不更新清算日期
			//map.put("clrgD",(String)map.get("/transaction/body/CmonConf/CmonConfInf/PrcDt"));
			LOGGER.debug("NCSGenalProcConfirmSrvBean更新表参数map：[{}]" , map.toString());
		}catch (Throwable e){
			throw new BaseException(RespCd.NCPO0101,e,MsgCdOrTpEnum.MSGCD_900.getCode()+"报文解包异常");
		}
		return map;
		
	}
	

}

























